<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use App\Models\Car\CarCost;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarRate;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Illuminate\Support\Facades\DB;
use Modules\Car\Entities\CarOrder;

//车辆比例控制器
class CarCateController extends Controller
{
    use ResponseData,PayTrait;
    public function list(CarRate $carrate,Request $request){
        // $size = $request->input('size',10);
        $model = $carrate->query()->whereNull('deleted_at');
        if($request->filled('car_id')){
            $model = $model->where('car_id',$request->car_id);
        }
        if($request->filled('user_id')){
            $model = $model->where('user_id',$request->user_id);
        }
        // $data = $model->orderBy('id','desc')->paginate($size);
        if($request->filled('money')){
            $data = $model->orderBy('id','desc')->get();
            $ordermodel = CarOrder::query()->whereNotNull('ok_reconciliation_time')->where('gear_car_id', $request->car_id);
            if($request->filled('start')&&$request->filled('end')){
                $ordermodel = $ordermodel->whereBetween('ok_reconciliation_time',[$request->start,$request->end]);
            }
            $order = $ordermodel->get();
            return $this->success(compact('data', 'order'));
        }else{
            $data = $model->orderBy('id','desc')->get();
            return $this->success($data);
        }
    }
    public function store(CarRate $carrate,Request $request){
        // 新增之前先删除老的
        $carrate->query()->where('car_id', $request->car_id)->update(['deleted_at'=>now()]);
        // 按数据生成出盘存明细
        $contents = json_decode($request->contents);
        $data = [];
        for ($i=0;$i<count($contents);$i++){
            $data[$i]= [
                'created_at'=>now(),
                'car_id'=>$request->car_id,
                'car_series_id'=>$request->car_series_id,
                'user_id'=>$contents[$i]->id??0,
                'rate'=>$contents[$i]->rate??'0.00',
                'user_name'=>$contents[$i]->username??null,
                'plate_number'=>$request->plate_number,
                'car_name'=>$request->car_name,
            ];
        }

        if(count($data)>0){
            DB::table('car_rates')->insert($data);
        }
        return $this->success('添加成功');
        // $data = $request->all();
        // $model = $carrate->query()->create($data);
        // return $this->success($model->id);
    }
    public function edit(CarRate $carrate,Request $request){
        $data = $carrate->find($request->id);
        return $this->success($data);
    }
    public function update(CarRate $carrate,Request $request){
        $data = $carrate->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(CarRate $carrate,Request $request){
        $data = $carrate->find($request->id);
        $data->deleted_at = now();
        $data->save();
        return $this->success('删除成功');
    }
}
